<template>
	<view>
		<!-- <cover-image :showLoading="true" :src="goodsImageUrl" height="300px"></cover-image> -->
		<view>
			<u--image :showLoading="true" :src="goodsImageUrl" width="100%" height="500px">
			</u--image>
		</view>
		<u--text :text="goodsName" align='center' size='35px'></u--text>
		<view style="margin-right: 30rpx;">
			<u--text :text="'¥'+goodsPrice" align='right' size="25px"></u--text>
		</view>
		<view style="margin-left: 3%;display: inline-block;">
			<u--text text="商家信息" align='center' size="5px" color='#2c8fe6'></u--text>
			<u--image width='50px' height='50px' shape="circle" :src="goodsBelongtoUserImg"></u--image>
			<u--text :text="goodsBelongto" align='center' size="25px" color='#da2672'></u--text>
		</view>
		<u-notice-bar :text="tips"></u-notice-bar>
		<u-tabs :list="tabs" @click="chooseTab" itemStyle='height:12px;padding:25px'></u-tabs>
		<u-divider></u-divider>
		<view style="padding: 20rpx	;" v-show="parseShow">
			<u-parse class="u-border-bottom" :content="goodsDescription==''?'该商家暂未提供商品信息':goodsDescription"></u-parse>
		</view>
		<view v-show="moreGoodsShow">
			<u-row customStyle="margin-bottom: 10px;flex-wrap: wrap;" justify="space-between" gutter="17">
				<u-col span="6" v-for="item in indexGoods">
					<navigator class="u-border goods-item" :url="'/pages/index/showGoods/showGoods?id='+item.goodsId">
						<u--image width='100%' height="300rpx" :src="item.goodsImage">
							>
						</u--image>
						<view class="u-flex u-line-2">
							<view style="font-style: italic;">{{item.goodsName}}</view>
							<view style="color: brown;;">¥{{item.goodsPrice==0?'免费':item.goodsPrice}}</view>
						</view>
					</navigator>
				</u-col>
			</u-row>
		</view>
		<view class="r" style="margin-right: 8px;">
			<view style="margin-bottom: 10px;">
				<u-button type="primary" text="加入购物车" shape='circle' size='large' @click="toaddBuyCar"></u-button>
			</view>
			<u-button type="error" text="下单" shape='circle' size='large' @click="toaddOrder"></u-button>
		</view>

	</view>
</template>

<script>
	import {
		getGoodsById,
		getGoodsList,
		addOrder
	} from '@/config/api.js'
	export default {
		data() {
			return {
				parseShow: true,
				moreGoodsShow: false,
				indexGoods: {},
				tips: '提示信息：该商城系统暂未开发完毕，请勿下单',
				goodsBelongtoUserImg: '',
				goodsImageUrl: '',
				goodsName: '',
				goodsDescription: ``,
				goodsPrice: null,
				goodsBelongto: '',
				goodsId: null,
				tabs: [{
						name: '商品详情'
					},
					{
						name: '更多商品'
					},
				]

			}
		},
		onLoad(option) {
			getGoodsList({}).then((res) => {
					this.indexGoods = res.data
					console.log(res.data);
				}).catch(() => {

				}),
				console.log(option.id);
			uni.$u.http.get('/serviceUser/app_goods/getGoodsById', {
				params: {
					id: option.id
				},
			}).then(res => {
				console.log(res);
				this.goodsImageUrl = res.info.goodsImage;
				this.goodsName = res.info.goodsName;
				this.goodsDescription = res.info.goodsDescription;
				this.goodsPrice = res.info.goodsPrice;
				this.goodsBelongto = res.info.goodsBelongToUser;
				this.getUserAvatarByName();

			}).catch((error) => {
				console.log(error);
			})



		},
		methods: {
			chooseTab(index) {
				console.log(index.index);
				switch (index.index) {
					case 0:
						this.parseShow = true;
						this.moreGoodsShow = false;
						break;
					case 1:
						this.parseShow = false;
						this.moreGoodsShow = true;
						break;
				}
			},
			toaddBuyCar() {
				uni.$u.toast("进入加入购物车方法")
			},
			toaddOrder() {
				//将当前商品加入到当前用户的订单
				uni.$u.toast("进入加入订单方法")
				let token = '';
				let user = '';
				uni.getStorage({
					key: 'user_token',
					success: function(res) {
						token = res.data
					}
				})
				uni.getStorage({
					key: 'user_name',
					success: function(res) {
						user = res.data
					}
				})

				console.log(token);

				console.log(user);
				addOrder({
					"orderBelongtoUser": user,
					"orderGoodsImage": this.goodsImageUrl,
					"orderGoodsName": this.goodsName,
					"orderGoodsPrice": this.goodsPrice,
				}, {
					params: {
						token: token
					}
				}).then(res => {
					uni.$u.toast("下单成功，请到您的订单列表查看")
				}).catch((e) => {
					console.log(e);
				})
			},
			getUserAvatarByName() {
				uni.$u.http.get('/serviceUser/user/getUserAvatarByName', {
					params: {
						name: this.goodsBelongto
					},
				}).then(res => {
					console.log(res);
					this.goodsBelongtoUserImg = res.url
				}).catch((error) => {
					console.log(error);
				})
			}
		}
	}
</script>

<style>
	.r {
		bottom: 3px;
		right: 0px;
		position: fixed;
	}
</style>
